<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
/**
 * This is the model class for table "product".
 *
 * @property integer $pid
 * @property integer $type_id
 * @property string $name
 * @property string $spec
 * @property integer $hot
 * @property double $market_val
 * @property double $cost
 * @property double $return_rate
 * @property double $start_price
 * @property double $scope
 * @property double $fee
 * @property integer $degree
 * @property double $lowest_price
 * @property double $highest_price
 * @property integer $actcount
 * @property integer $reacount
 * @property integer $can_robot
 * @property integer $have_cost
 * @property integer $ios
 * @property integer $can_give
 * @property integer $kind
 * @property integer $can_show
 * @property integer $state
 * @property integer $created_at
 * @property integer $updated_at
 */
class Product extends ActiveRecord
{
    const CAN_ROBOT = 1;
    const NOT_ROBOT = 0;

    const HAVE_COST = 1;
    const NOT_COST = 0;

    const IOS = 1;
    const NOT_IOS = 0;

    const CAN_GIVE = 1;
    const NOT_GIVE = 0;

    const CAN_SHOW = 1;
    const NOT_SHOW = 0;

    const Material_Object = 1;
    const Vitual_Goods = 0;

    const ON = 1;
    const OFF = 0;

    public static function dropdown($column, $value = null)
    {
        $dropDownList = [
            'can_robot'=> [
                self::CAN_ROBOT=>'机器人拍中',
                self::NOT_ROBOT=>'用户拍中',
            ],
            'have_cost'=> [
                self::HAVE_COST=>'考虑',
                self::NOT_COST=>'不考虑',
            ],
            'ios'=>[
                self::IOS=>'上架',
                self::NOT_IOS=>'不上架'
            ],
            'can_show'=>[
                self::CAN_SHOW=>'展示',
                self::NOT_SHOW=>'不展示',
            ],
            'kind'=>[
                self::Material_Object=>'实物',
                self::Vitual_Goods=>'虚拟商品',
            ],
            'can_give'=>[
                self::CAN_GIVE=>'可以使用赠币',
                self::NOT_GIVE=>'只能使用拍币',
            ],
            'state'=>[
                self::ON=>'上架',
                self::OFF=>'下架',
            ]
        ];
        if ($value !== null)
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column][$value] : false;
        else
            return array_key_exists($column, $dropDownList) ? $dropDownList[$column] : false;
    }



    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                'attributes'=>[
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ]
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'product';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['type_id', 'name', 'hot', 'market_val', 'cost', 'return_rate', 'start_price', 'scope', 'spec','fee', 'degree', 'lowest_price', 'highest_price', 'actcount', 'reacount', 'can_robot', 'have_cost', 'ios', 'can_give', 'kind', 'can_show', 'state'], 'required'],
            [['type_id', 'hot', 'degree', 'actcount', 'reacount', 'can_robot', 'have_cost', 'ios', 'can_give', 'kind', 'can_show', 'state'], 'integer'],
            [['market_val', 'cost', 'return_rate', 'start_price', 'scope', 'fee', 'lowest_price', 'highest_price'], 'number'],
            [['name'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'pid' => 'ID',
            'type_id' =>'分类',
            'name' => '名称',
            'spec'=>'规格',
            'hot' => '人气值',
            'market_val' => '市场价',
            'cost' => '成本',
            'return_rate' => '回收率',
            'start_price' => '起拍价',
            'scope' => '幅度',
            'fee' => '手续费',
            'degree' => '默认竞拍次数',
            'lowest_price' => '最低成交价',
            'highest_price' => '最高成交价',
            'actcount' => '竞拍倒计时',
            'reacount' => '准备开始倒计时',
            'can_robot' =>'让机器人拍中',
            'have_cost' => '考虑成本',
            'ios' => 'ios屏蔽',
            'can_give' => '可以使用赠币',
            'kind' => '发货类型',
            'can_show' => '展示到首页',
            'state' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
        ];
    }

    public function getPics()
    {
        return $this->hasMany(Pic::className(),['product_id'=>'pid']);
    }

    public function getShoworders()
    {
        return $this->hasMany(Showorder::className(),['product_id'=>'pid']);
    }

    public function getAuctions()
    {
        return $this->hasMany(Auction::className(),['product_id'=>'pid']);
    }
}
